from PyQt5.QtWidgets import QWidget,QLineEdit,QToolButton,QHBoxLayout
from PyQt5 import QtWidgets
from PyQt5 import QtGui
from PyQt5 import QtCore
from PyQt5.QtCore import QPoint
from PyQt5.QtGui import QPaintEvent,QMouseEvent,QPainter,QPolygon,QColor,QPen,QLinearGradient,QGradient,QFont
import FormWidgetimage
class SearchLineEdit(QLineEdit):
    signalSearchContent = QtCore.pyqtSignal(str)
    def __init__(self,parent=None):
        super(SearchLineEdit, self).__init__(parent)
        self.initForm()
        self.initConnect()
        self.translator()

    def initForm(self):
        self.setFixedSize(260,24)
        self.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)

        self.tbnSearch = QToolButton(self)

        self.tbnSearch.setFixedSize(25,24)
        self.tbnSearch.setAutoRaise(True)
        self.tbnSearch.setCursor(QtCore.Qt.PointingHandCursor)
        self.tbnSearch.setStyleSheet("QToolButton{border-image:url(:/image/search_dark.png);"
                               "background:transparent;}")

        mainLayout = QHBoxLayout()
        mainLayout.addStretch()
        mainLayout.addWidget(self.tbnSearch)

        self.setTextMargins(0,0,20,0)
        self.setContentsMargins(0,0,0,0)
        mainLayout.setContentsMargins(2,2,4,0)
        self.setLayout(mainLayout)
        self.setStyleSheet("height:24px;border:1px ;border-radius:6px;"
                  "background-color:#E1DDDD");

    def initConnect(self):
        self.tbnSearch.clicked.connect(self.slotSendSearchContent)
        self.returnPressed.connect(self.slotSendSearchContent)
    def translator(self):
        self.setPlaceholderText("请输入要查找的内容")
        self.setToolTip("查找内容")
        self.tbnSearch.setToolTip("查找")
    def focusInEvent(self, e: QtGui.QFocusEvent):
        super(SearchLineEdit, self).focusInEvent(e)

        self.clear()
        self.setPlaceholderText("")
        self.setStyleSheet("height:24px;border:1px ;border-radius:6px;"
                      "background-color:#CCC000");
        self.tbnSearch.setStyleSheet("QToolButton{border-image:url(:/image/search_dark.png);"
                                   "background:transparent;}");

    def focusOutEvent(self, e: QtGui.QFocusEvent):
        super(SearchLineEdit, self).focusOutEvent(e)
        self.setPlaceholderText("请输入要查找的内容");
        self.setStyleSheet("height:24px;border:1px ;border-radius:6px;"
                      "background-color:#CCC7C7");
        self.tbnSearch.setStyleSheet("QToolButton{border-image:url(:/image/search_dark.png);"
                                   "background:transparent;}");
    def slotSendSearchContent(self):
        str = ""
        if self.text() == "":
            str = self.placeholderText()
        else:
            str = self.text()
        self.signalSearchContent.emit(str)


































